package com.leyou.contrlloer;

import com.leyou.dto.BrandDto;
import com.leyou.dto.CategoryDto;
import com.leyou.dto.PageResult;
import com.leyou.exception.LyException;
import com.leyou.exception.domain.ExceptionEnum;
import com.leyou.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping
public class BrandController {

    @Autowired
    BrandService brandService;

    @RequestMapping("/brand/page")
    public ResponseEntity<PageResult<BrandDto>> findBrand(
            @RequestParam("key") String key, @RequestParam("page") Integer page,
            @RequestParam("rows") Integer rows, @RequestParam("sortBy") String sortBy,
            @RequestParam("desc") Boolean desc) {
        PageResult<BrandDto> pageResult = brandService.findBrand(key, page, rows, sortBy, desc);
        //封装分页对象
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 保存
     *
     * @return 状态信息
     */
    @PostMapping("/brand")
    public ResponseEntity<Void> save(BrandDto brandDto, @RequestParam("cids") List<Long> cids) {
        brandService.saveBrand(brandDto, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @GetMapping("/brand/{id}")
    public ResponseEntity<BrandDto> findById(@PathVariable("id") Long id) {
        BrandDto brandDto = brandService.findBrandById(id);
        return ResponseEntity.ok(brandDto);
    }

    @GetMapping("/brand/of/category")
    public ResponseEntity<List<BrandDto>> findBrandByCId(@RequestParam("id") Long cid) {
        List<BrandDto> brandDtoList = brandService.findBrandByCid(cid);
        return ResponseEntity.ok(brandDtoList);
    }

}
